package tu;
public class AdjGraph {
        Character[] vexes;//顶点数组
        int edgenum;//边的条数
        int [][]adj;//邻接矩阵
        public AdjGraph(Character[] v) {//顶点个数参数
            vexes=v;
            adj=new int[vexes.length][vexes.length];
        }
        //添加一条边
        public void putEdge(Edge edge) throws Exception{
            int v1 = edge.vex1;
            int v2 = edge.vex2;
            //异常判断
            if(v1<0||v2<0||v1>vexes.length||v2>vexes.length)
                throw new Exception("无效节点编号");
            if(adj[v1][v2]==0){//没边则加边
                adj[v1][v2] = 1;
                adj[v2][v1]=1;//无向图，对称赋值
                edgenum++;//边条数+1
            }

        }
        //减少一条边
        public void removeEdge(Edge edge) throws Exception {
            int v1 = edge.vex1;
            int v2 = edge.vex2;
            if (v1 < 0 || v2 < 0 || v1 > vexes.length || v2 > vexes.length){
                throw new Exception("无效节点编号");
                }
            if(adj[v1][v2]==1){//有边减边
                adj[v1][v2] = 0;
                adj[v2][v1]=0;//无向图，对称赋值
                edgenum--;//边条数+1
            }
        }
        //求顶点1的度数
        public int degree(int i)throws Exception{
            if(i<0||i>=vexes.length){
                throw new Exception("无效节点编号");
            }
            int count=0;
            for (int j = 0; j < vexes.length; j++) {
                if(adj[i][j]==1){
                    count++;
                }
            }
            return count;
        }
}
